@tool
@icon("../icons/expression_guard.svg")
class_name Guard_Expression
extends Guard

var expression:String = ""

func is_satisfied(transition:Transition,state:State) -> bool:
	var chart:StateChart = state.chart
	if not is_instance_valid(chart):
		push_error("找不到StateChart，无法求表达式的值")
		return false
	var result = ExpressionUtil.evaluate_expression("guard in " + DebugUtil.path_of(transition),chart,expression,false)
	if typeof(result) != TYPE_BOOL:
		push_error("表达式{0}结果为{1}，不是bool，返回false".format([expression,result]))
		return false
	return result
	
func _get_property_list() -> Array[Dictionary]:
	var properties:Array[Dictionary] = []
	properties.append({
		"name": "expression",
		"type": TYPE_STRING,
		"usage": PROPERTY_USAGE_DEFAULT,
		"hint": PROPERTY_HINT_EXPRESSION
	})
	return properties

func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
